/**
 * 
 */
package metalink.admin;

import java.util.ArrayList;
import java.util.Vector;

import vo.LibraryBookDetailsVo;
import model.admin.LibraryMo;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Sony
 * 
 */

public class ListBooks extends ActionSupport {
	ArrayList<LibraryBookDetailsVo> allBooks;
	String view, category, book_id;
	private int result;

	// ------------ Pagination --------------//
	private int page;
	private String morepages;
	private Vector noofpages = new Vector();
	private int nextpage;
	private int currentpage;
	private String hasprevpage;
	private int prevpage;
	private int items;
	// private Vector pagelist=new Vector();
	private ArrayList<LibraryBookDetailsVo> pagelist;

	public String execute() {

		System.out.println("view = " + view + " category=" + category);
		setView(view);
		setCategory(category);
		pagelist = new ArrayList<LibraryBookDetailsVo>();
		// ------------ Pagination --------------//

		int flag = 0;

		// --------------------------------------//

		if (view.equals("category")) {
			LibraryMo mo = new LibraryMo();
			allBooks = new ArrayList<LibraryBookDetailsVo>();
			allBooks = mo.bookList();
			flag = 1;
		} else if (view.equals("books")) {
			LibraryMo mo = new LibraryMo();
			allBooks = new ArrayList<LibraryBookDetailsVo>();
			allBooks = mo.allBooksList(category);
			flag = 1;
		} else if (view.equals("all")) {
			System.out.println("Listing all books");
			LibraryMo mo = new LibraryMo();
			allBooks = new ArrayList<LibraryBookDetailsVo>();
			allBooks = mo.allBooks();
			flag = 1;
		}
		try {
			result = allBooks.size();
			System.out.println("number of books are : " + result);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// ---------- Pagination ------------------//

		int p = getPage();
		if (p == 0) {
			setHasprevpage("false");
		} else {
			setHasprevpage("true");
			setPrevpage(p - 1);
		}

		setCurrentpage(p);

		int count;
		count = 0;

		int size = allBooks.size();
		setItems(size);
		int pagesinlist = size / 15;
		while (count <= pagesinlist) {
			noofpages.add(count);
			count++;
		}
		count = 0;
		while (count < 15) {

			if (size >= (p * 15) + count + 1) {
				// System.out.println(p);
				pagelist.add(allBooks.get((p * 15) + count));
				// System.out.println((p*15)+count);
				// System.out.println("size"+pagelist.size());
				count++;
				setMorepages("true");
				setNextpage(p + 1);

			} else {
				setMorepages("false");
				break;
			}
		}

		// ----------------------------------------//
		if (flag == 1)
			return SUCCESS;
		else
			return ERROR;
	}

	public String getBook_id() {
		return book_id;
	}

	public void setBook_id(String book_id) {
		this.book_id = book_id;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public ArrayList<LibraryBookDetailsVo> getAllBooks() {
		return allBooks;
	}

	public void setAllBooks(ArrayList<LibraryBookDetailsVo> allBooks) {
		this.allBooks = allBooks;
	}

	public String getView() {
		return view;
	}

	public void setView(String view) {
		this.view = view;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getMorepages() {
		return morepages;
	}

	public void setMorepages(String morepages) {
		this.morepages = morepages;
	}

	public Vector getNoofpages() {
		return noofpages;
	}

	public void setNoofpages(Vector noofpages) {
		this.noofpages = noofpages;
	}

	public int getNextpage() {
		return nextpage;
	}

	public void setNextpage(int nextpage) {
		this.nextpage = nextpage;
	}

	public int getCurrentpage() {
		return currentpage;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public String getHasprevpage() {
		return hasprevpage;
	}

	public void setHasprevpage(String hasprevpage) {
		this.hasprevpage = hasprevpage;
	}

	public int getPrevpage() {
		return prevpage;
	}

	public void setPrevpage(int prevpage) {
		this.prevpage = prevpage;
	}

	public int getItems() {
		return items;
	}

	public void setItems(int items) {
		this.items = items;
	}

	public ArrayList<LibraryBookDetailsVo> getPagelist() {
		return pagelist;
	}

	public void setPagelist(ArrayList<LibraryBookDetailsVo> pagelist) {
		this.pagelist = pagelist;
	}

	// --------------------------------------//

}
